package com.liuheng.spzx.product.controller;

import com.liuheng.spzx.model.entity.product.Category;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/product/category")
public class CategoryController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/findCategoryTree")
    public Result findCategoryTree(){
        List<Category> categoryData = (List<Category>) redisTemplate.opsForValue().get("categoryData");
        if (categoryData==null) {
              categoryData = categoryService.findCategoryTree();
            redisTemplate.opsForValue().set("categoryData",categoryData);
        }
        return Result.build(categoryData, ResultCodeEnum.SUCCESS);
    }

}
